package com.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dao.IStudentDao;
import com.entity.School;
import com.entity.Student;
import com.tools.Page;

/**
 * 学生的dao
 * @author Administrator
 *
 */
public class StudentDao extends BaseDao implements IStudentDao{
	public StudentDao(int type) {
		super(type);
	}

	/* (non-Javadoc)
	 * @see com.dao.IStudentDao#save(com.entity.Student)
	 */
	@Override
	public boolean save(Student student) throws SQLException{
		String sql="insert into student values(?,?,?,?,?)";
		ps=con.prepareStatement(sql);
		
		ps.setString(1, student.getId());
		ps.setString(2, student.getName());
		ps.setInt(3, student.getAge());
		ps.setDouble(4, student.getResult());
		ps.setString(5, student.getSchool().getId());
		int num = ps.executeUpdate();
		
		if(num==1){
			return true;
		}
		else{
			return false;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.dao.IStudentDao#update(com.entity.Student)
	 */
	@Override
	public boolean update(Student student) throws SQLException{
		String sql="update student set name=?,age=?,result=?,school_id=? where id=?";
		ps=con.prepareStatement(sql);
		
		ps.setString(1, student.getName());
		ps.setInt(2, student.getAge());
		ps.setDouble(3, student.getResult());
		ps.setString(4, student.getSchool().getId());
		ps.setString(5, student.getId());
		int num = ps.executeUpdate();
		
		if(num==1){
			return true;
		}
		else{
			return false;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.dao.IStudentDao#findById(java.lang.String)
	 */
	@Override
	public Student findById(String id) throws SQLException{
		String sql="select * from student where id=?";
		ps=con.prepareStatement(sql);
		
		ps.setString(1, id);
		
		rs=ps.executeQuery();
		
		if(rs.next()){
			Student student=new Student();
			student.setId(rs.getString("id"));
			student.setName(rs.getString("name"));
			student.setAge(rs.getInt("age"));
			student.setResult(rs.getDouble("result"));
			student.setSchool(new School(rs.getString("school_id")));
			return student;
		}
		return null;
	}
	
	/* (non-Javadoc)
	 * @see com.dao.IStudentDao#deleteById(java.lang.String)
	 */
	@Override
	public boolean deleteById(String id) throws SQLException{
		String sql="delete from student where id=?";
		ps=con.prepareStatement(sql);
		
		ps.setString(1, id);
		
		int num=ps.executeUpdate();
		
		if(num==1){
			return true;
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see com.dao.IStudentDao#getList(com.tools.Page)
	 */
	@Override
	public List<Student> getList(Page page) throws SQLException{
		String sql="select * from student order by school_id,name limit ?,?";
		ps=con.prepareStatement(sql);
		
		ps.setLong(1, page.getIndex());
		ps.setLong(2, page.getSize());
		
		rs=ps.executeQuery();
		
		List<Student> list=new ArrayList<>();
		
		while(rs.next()){
			Student student=new Student();
			student.setId(rs.getString("id"));
			student.setName(rs.getString("name"));
			student.setAge(rs.getInt("age"));
			student.setResult(rs.getDouble("result"));
			student.setSchool(new School(rs.getString("school_id")));
			list.add(student);
		}
		return list;
	}
	
	/* (non-Javadoc)
	 * @see com.dao.IStudentDao#count()
	 */
	@Override
	public long count() throws SQLException{
		String sql="select count(*) from student";
		ps=con.prepareStatement(sql);
		
		
		rs=ps.executeQuery();
		
		
		if(rs.next()){
			return rs.getLong(1);
		}
		return 0;
	}
}
